import * as bookAlbumModel from '../../models/book_album';
import * as bookContentModel from '../../models/book_content';
import * as bookCategoryModel from '../../models/book_category';
import * as bookCategoryRelationModel from '../../models/book_category_relation';
import { ValidException, EmptyException, SystemException } from '../../helps/exception';

export function* listAlbum({ page, number, order, direction }) {
  return yield bookAlbumModel.list(page, number, order, direction);
}

export function* addAlbum({ data }) {
  const album = yield bookAlbumModel.add(data);
  if (!album) throw SystemException('操作失败');
  return { id: album.id };
}

export function* getAlbum({ album_id }) {
  if (!album_id) throw ValidException('id 不能为空');
  const album = yield bookAlbumModel.get(album_id);
  if (!album) throw EmptyException('无相关信息');
  return album;
}

export function* putAlbum({ album_id, data }) {
  if (!album_id) throw ValidException('id 不能为空');
  const b = yield bookAlbumModel.put(album_id, data);
  if (!b) throw SystemException('操作失败');
  return { id: album_id };
}

export function* delAlbum({ album_id }) {
  if (!album_id) throw ValidException('id 不能为空');
  const b = yield bookAlbumModel.del(album_id);
  if (!b) throw SystemException('操作失败');
  return { id: album_id };
}

export function* listContent({ album_id, page, number, order, direction }) {
  if (!album_id) throw ValidException('id 不能为空');
  return yield bookContentModel.listByAlbum(album_id, page, number, order, direction);
}

export function* addContent({ album_id, data }) {
  if (!album_id) throw ValidException('id 不能为空');
  const album = yield bookContentModel.add({ album_id, ...data });
  if (!album) throw SystemException('操作失败');
  return { id: album.id };
}

export function* getContent({ content_id }) {
  if (!content_id) throw ValidException('id 不能为空');
  const content = yield bookContentModel.get(content_id);
  if (!content) throw EmptyException('无相关信息');
  return content;
}

export function* putContent({ content_id, data }) {
  if (!content_id) throw ValidException('id 不能为空');
  const b = yield bookContentModel.put(content_id, data);
  if (!b) throw SystemException('操作失败');
  return { id: content_id };
}

export function* delContent({ content_id }) {
  if (!content_id) throw ValidException('id 不能为空');
  const b = yield bookContentModel.del(content_id);
  if (!b) throw SystemException('操作失败');
  return { id: content_id };
}

export function* listCategory({ page, number, order, direction }) {
  return yield bookCategoryModel.list(page, number, order, direction);
}

export function* addCategory({ data }) {
  const category = yield bookCategoryModel.add(data);
  if (!category) throw SystemException('操作失败');
  return { id: category.id };
}

export function* getCategory({ category_id }) {
  if (!category_id) throw ValidException('id 不能为空');
  const category = yield bookCategoryModel.get(category_id);
  if (!category) throw EmptyException('无相关信息');
  return category;
}

export function* putCategory({ category_id, data }) {
  if (!category_id) throw ValidException('id 不能为空');
  const b = yield bookCategoryModel.put(category_id, data);
  if (!b) throw SystemException('操作失败');
  return { id: category_id };
}

export function* delCategory({ category_id }) {
  if (!category_id) throw ValidException('id 不能为空');
  const b = yield bookCategoryModel.del(category_id);
  if (!b) throw SystemException('操作失败');
  return { id: category_id };
}

export function* listAlbumCategory({ album_id }) {
  if (!album_id) throw ValidException('id 不能为空');
  return yield bookCategoryRelationModel.listByAlbum(album_id);
}

export function* addAlbumCategory({ album_id, category_id }) {
  if (!album_id || !category_id) throw ValidException('id 不能为空');
  const album = yield bookCategoryRelationModel.add({ album_id, category_id });
  if (!album) throw SystemException('操作失败');
  return { id: album.id };
}

export function* delAlbumCategory({ album_id, relation_id }) {
  if (!album_id || !relation_id) throw ValidException('id 不能为空');
  const b = yield bookCategoryRelationModel.del(relation_id);
  if (!b) throw SystemException('操作失败');
  return { id: relation_id };
}
